var SCORE = 0;

$(function() {
	var loading = weui.loading('loading', {
		className: 'custom-classname',
	});
	var orderId = Common.fn.getUrlParam('oid');
	if (!orderId) {
		location.href = '../views/mine.html';
	}
	var result = Model.gift.orderDetail({
		orderId: orderId,
	});
	if (!sessionStorage.getItem('hmdj_stopError')) {
		sessionStorage.setItem('hmdj_stopError', true);
	} else {
		if (result.code) {
			Common.fn.formatOutput(result);
			loading.hide();
			return false;
		}
	}
    // 设置页面信息
    var res = result.data[0];
	$('.order-status').text(res.orderInfo.state);
	$('.order-id').text(res.orderInfo.orderId);
	$('.order-count').text(res.orderInfo.price + '红米');
	$('.user-nickname').text(res.orderInfo.receiver);
	$('.user-phone').text(res.orderInfo.phone);
	$('.user-address').text(res.orderInfo.address);
	$('.portrait-img').attr('src', res.giftInfo.picture);
	$('.order-good-title').text(res.giftInfo.name);
	$('.goods-price').text(res.orderInfo.price + '红米');
	$('.toShopBtn').attr('href', '../integralShop/gift_detail.html?id=' + res.giftInfo.giftId);
	switch (res.orderInfo.state) {
		case '已兑换':
            $('.order-footer-get').css('display', 'none');
            $('.order-footer-payoff').css('display', 'none');
			$('.wuliu-order').text('暂无');
			$('.wuliu-copy-btn').css('display', 'none');
            $('.goods-detail').css('cursor', 'pointer');
            $('.apply-refund-btn').attr('data-id', res.orderInfo.orderId);
			break;
		case '已发货':
            $('.apply-refund-btn').css('display', 'none');
            $('.wuliu-order').text(res.orderInfo.express + ' ' + res.orderInfo.expressOrder);
            $('.order-footer-payoff').css('display', 'none');
			$('.m-footer-buy-btn').attr('data-id', res.orderInfo.orderId);
			break;
		case '已完成':
            $('.wuliu-order').text(res.orderInfo.express + ' ' + res.orderInfo.expressOrder);
            $('.apply-refund-btn').css('display', 'none');
			$('.s-footer').css('display', 'none');
			break;
        case '已废弃':
            $('.apply-refund-btn').css('display', 'none');
			$('.order-footer-get').css('display', 'none');
			$('.order-footer-payoff').css('display', 'none');
			$('.wuliu-order').text('暂无');
            $('.wuliu-copy-btn').css('display', 'none');
            $('.s-footer').css('display', 'none');
			break;
		default:
			$('.apply-refund-btn').css('display', 'none');
			$('.wuliu-order').text('暂无');
			$('.wuliu-copy-btn').css('display', 'none');
			$('.s-footer').css('display', 'none');
			break;
	}
	loading.hide();
});

// 复制订单编号
$('#copy-orderId-btn').on('click', function() {
	var orderId = $('.order-id').text();
	var clipboard = new Clipboard('#copy-orderId-btn', {
		text: function text() {
			return orderId;
		},
	});
	clipboard.on('success', function(e) {
		weui.toast('已成功复制到剪贴板', {
			duration: 2000,
			callback: function callback() {},
		});
	});
	clipboard.on('error', function(e) {
		weui.topTips('复制剪贴板失败，请手动复制');
	});
});

// 复制快递信息
$('#wuliu-copy-btn').on('click', function() {
	var wuliu = $('.wuliu-order').text();
	var clipboard = new Clipboard('#wuliu-copy-btn', {
		text: function text() {
			return wuliu;
		},
	});
	clipboard.on('success', function(e) {
		weui.toast('已成功复制到剪贴板', {
			duration: 2000,
			callback: function callback() {},
		});
	});
	clipboard.on('error', function(e) {
		weui.topTips('复制剪贴板失败，请手动复制');
	});
});

// 申请退款
$(document).on('click', '.apply-refund-btn', function() {
	var orderId = $(this).attr('data-id');
	Common.fn.giftBackForOrder(orderId);
});

// 确认收货
$(document).on('click', '.m-footer-buy-btn', function() {
	var orderId = $(this).attr('data-id');
	Common.fn.giftConfirm(orderId);
});
